package com.qqtech.qquan.user.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.sys.controller.BaseAdminController;
import com.qqtech.qquan.user.enums.UserMoneyLogTypeDictEnum;
import com.qqtech.qquan.user.model.UserValue;
import com.qqtech.qquan.user.service.UserMoneyLogService;
import com.qqtech.qquan.user.service.UserValueService;

@Controller
@RequestMapping(value = "/user/value/task")
public class UserValueTaskController extends BaseAdminController {

	@Resource
	private UserMoneyLogService userMoneyLogService;
	@Resource
	private UserValueService userValueService;

	@Override
	protected BaseService<UserValue> getBaseService() {
		return userValueService;
	}

	/**
	 * 决定在该版本(1.2.31)把之前的圈币全部转化为余额，后续得到的圈币将不能在商家消费
	 * 
	 * @param orderNo
	 * @return
	 */
	@RequestMapping(value = "/changeQuanToMoney")
	@ResponseBody
	public Result changeQuanToMoney(String orderNo) {
		int i = 0;
		Result result = new Result();
		List<UserValue> all = userValueService.queryAll();
		for (UserValue userValue : all) {
			Integer quanValue = userValue.getQuanValue();
			if (quanValue != null && quanValue != 0) {
				// 先把所有圈币归零
				userMoneyLogService.addMoneyToUser(userValue.getId(), -quanValue, "圈币调整-圈币已转移到余额",
						UserMoneyLogTypeDictEnum.QUAN);
				// 再往余额增加相当于圈币的数量值
				userMoneyLogService.addMoneyToUser(userValue.getId(), quanValue, "圈币调整-圈币已转移到余额",
						UserMoneyLogTypeDictEnum.MONEY);
				i++;
			}
		}
		result.put("message", "一共完成" + i + "个用户的圈币转化为余额");
		return result;
	}
}
